--[[
* MapLib 2.0
* File: <AddOn>\MapLib\MapLib_SideMenu.lua
* $Revision: 132 $
* $Date: 2007-01-06 13:08:56 +0000 (Sat, 06 Jan 2007) $
* 
* Copyright (c) 2006-2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* 
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]--

local vmajor, vminor = "MapLib-2.0", tonumber(string.sub("$Revision: 132 $", 12, -3))

-- Version check: Only one major instance of MapLib can be installed, if another already is: abort.
if (MapLib and MapLib.SideMenu and (MapLib.SideMenu.vmajor ~= vmajor or MapLib.SideMenu.vminor >= vminor)) then return; end;

if (not MapLib) then MapLib = {}; end;
if (not MapLib.SideMenu) then MapLib.SideMenu = {}; end;
if (not MapLib.SideMenu.Event) then MapLib.SideMenu.Event = {}; end;

MapLib.SideMenu.vmajor = vmajor;
MapLib.SideMenu.vminor = vminor;

MapLib.SideMenu.NUM_OVERLAYS = 0;

MapLib.SideMenu.verticalScrollvalue = MapLib.SideMenu.verticalScrollvalue or 0;

MapLib.SideMenu.SetVerticalScroll = function(toLeft)
	if (not toLeft) then toLeft = MapLib.SIDEMENU_WIDTH; end; -- this resets to normal position
	if (not (toLeft >= 0 and toLeft <= MapLib.SIDEMENU_WIDTH)) then return; end;
	MapLibDetailFrame:SetPoint("TOPLEFT", MapLibPositioningGuide, "TOP", -502+MapLib.SIDEMENU_WIDTH-toLeft, -69);
	MapLibSideMenuMapOverlayTexture:SetPoint("TOPLEFT", MapLibSideMenuMapOverlayButton, "TOPLEFT", toLeft/MapLib.SIDEMENU_WIDTH*(MapLibSideMenuMapOverlayButton:GetWidth()-MapLibSideMenuMapOverlayTexture:GetWidth()), 0);
	MapLib.SideMenu.verticalScrollvalue = MapLib.SIDEMENU_WIDTH - toLeft;
end;

MapLib.SideMenu.Event.MAPLIB_UPDATE_MAP = function()
	MapLib.SideMenu.UpdateMap(MapLib.mapShown);
end;

MapLib.EventHandler.RegisterEvent("MAPLIB_SIDEMENU", "MAPLIB_UPDATE_MAP", MapLib.SideMenu.Event.MAPLIB_UPDATE_MAP);

MapLib.SideMenu.OnShow = function()
	MapLib.SideMenu.SetVerticalScroll(MapLib.SIDEMENU_WIDTH);
	MapLibTopCenterAnchor:SetPoint("TOP", MapLibPositioningGuide, "TOP", MapLib.SIDEMENU_WIDTH/2, -69);
	MapLib.SideMenu.UpdateMap(MapLib.mapShown);
end;

MapLib.SideMenu.OnHide = function()
	MapLib.SideMenu.SetVerticalScroll();
	MapLibTopCenterAnchor:SetPoint("TOP", MapLibPositioningGuide, "TOP", 0, -69);
end;

MapLib.SideMenu.RegisterMenu = function(name, frame, background, activeButtonTexture, inactiveButtonTexture)
	MapLib.XML.CreateSideMenuButton(name, frame, background, activeButtonTexture, inactiveButtonTexture);
	frame:SetParent(MapLibSideMenu);
end;

MapLib.SideMenu.TabOnClick = function()
	MapLib.SideMenu.SetTabToID(this:GetID());
end;

MapLib.SideMenu.SetFullscreenMode = function()
	MapLib.SideMenu.SetTabToID(MapLib.SideMenu.currentTab);
	MapLibSideMenuFullscreen:Show();
	MapLibSideMenuWindowed:Hide();
end;

MapLib.SideMenu.SetWindowedMode = function()
	MapLib.SideMenu.SetTabToID(MapLib.SideMenu.currentTab);
	MapLibSideMenuWindowed:Show();
	MapLibSideMenuFullscreen:Hide();
end;

MapLib.SideMenu.SetTabToID = function(id)
	for i=1, MapLib.NUM_SIDEMENUBUTTONS, 1 do
		if (i == id) then
			-- diable button, set to active
			getglobal("MapLibSideMenuTabButton"..i):Disable();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveLeft"):Hide();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveMiddle"):Hide();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveRight"):Hide();
			getglobal("MapLibSideMenuTabButton"..i.."TextureLeft"):Show();
			getglobal("MapLibSideMenuTabButton"..i.."TextureMiddle"):Show();
			getglobal("MapLibSideMenuTabButton"..i.."TextureRight"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i):Disable();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveLeft"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveMiddle"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveRight"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureLeft"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureMiddle"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureRight"):Show();
			-- show frame
			local sideMenuFrame = getglobal("MapLibSideMenuTabButton"..i).sideMenuFrame;
			sideMenuFrame:Show();
			-- reanchor according to fullscreen or not:
			if (MapLibFrame.isInWindowMode) then
				sideMenuFrame:SetPoint("TOPLEFT", MapLibSideMenuWindowed, "TOPLEFT", 12, -18);
			else
				sideMenuFrame:SetPoint("TOPLEFT", MapLibSideMenuFullscreen, "TOPLEFT", 9, -221);
			end;
		else
			-- enable button, set to inactive
			getglobal("MapLibSideMenuTabButton"..i):Enable();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveLeft"):Show();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveMiddle"):Show();
			getglobal("MapLibSideMenuTabButton"..i.."TextureInActiveRight"):Show();
			getglobal("MapLibSideMenuTabButton"..i.."TextureLeft"):Hide();
			getglobal("MapLibSideMenuTabButton"..i.."TextureMiddle"):Hide();
			getglobal("MapLibSideMenuTabButton"..i.."TextureRight"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i):Enable();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveLeft"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveMiddle"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureInActiveRight"):Show();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureLeft"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureMiddle"):Hide();
			getglobal("MapLibSideMenuWindowedTabButton"..i.."TextureRight"):Hide();
			-- hide frame
			getglobal("MapLibSideMenuTabButton"..i).sideMenuFrame:Hide();
		end;
	end;
	MapLib.SideMenu.currentTab = id;
end;

MapLib.SideMenu.ToggleButtonOnClick = function()
	if (not MapLibSideMenu:IsVisible()) then
		-- show it
		MapLibSideMenu:Show();
		-- reposition button
		MapLibSideMenuToggleButton:SetPoint("TOPLEFT", MapLibPositioningGuide, "TOP", -128, -85);
		-- retexture
		MapLibSideMenuToggleButtonTexture:SetTexture(MapLib.PATH.."\\ARTWORK\\SideMenu-Button-Close-Enabled");
	else
		-- hide it
		MapLibSideMenu:Hide();
		-- reposition button
		MapLibSideMenuToggleButton:SetPoint("TOPLEFT", MapLibPositioningGuide, "TOP", -510, -85);
		-- retexture
		MapLibSideMenuToggleButtonTexture:SetTexture(MapLib.PATH.."\\ARTWORK\\SideMenu-Button-Open-Enabled");	end;
end;

MapLib.SideMenu.MapOnUpdate = function()
	local adjustedX, adjustedY = MapLib.GetAdjustedCursorPosition(MapLibSideMenuMapOverlayButton);
	local overlaywidth = MapLibSideMenuMapOverlayTexture:GetWidth();
	local width = MapLibSideMenuMapOverlayButton:GetWidth();
	adjustedX = adjustedX * width;
	if (adjustedX <= overlaywidth / 2) then
		MapLib.SideMenu.SetVerticalScroll(0);
	elseif (adjustedX >= width - overlaywidth / 2) then
		MapLib.SideMenu.SetVerticalScroll(MapLib.SIDEMENU_WIDTH);
	else
		MapLib.SideMenu.SetVerticalScroll( (adjustedX - overlaywidth / 2) / (width - overlaywidth) * MapLib.SIDEMENU_WIDTH);
	end;
end;

MapLib.SideMenu.UpdateMap = function(mapID)
	-- Dont update if the frame is not shown.
	if (not MapLibSideMenu:IsShown()) then return; end;
	
	for i=1, NUM_WORLDMAP_DETAIL_TILES, 1 do
		getglobal("MapLibSideMenuDetailTile"..i):SetTexture(MapLib.mapInfo[mapID].texturePath..i);
	end;

	local numOverlays = MapLib.mapInfo[mapID].getNumMapOverlays();
	local textureName, textureWidth, textureHeight, offsetX, offsetY, mapPointX, mapPointY;
	local textureCount = 0, neededTextures;
	local texture;
	local texturePixelWidth, textureFileWidth, texturePixelHeight, textureFileHeight;
	local numTexturesWide, numTexturesTall;
	for i=1, numOverlays do
		textureName, textureWidth, textureHeight, offsetX, offsetY, mapPointX, mapPointY = MapLib.mapInfo[mapID].getMapOverlayInfo(i);
		numTexturesWide = ceil(textureWidth/256);
		numTexturesTall = ceil(textureHeight/256);
		neededTextures = textureCount + (numTexturesWide * numTexturesTall);
		if ( neededTextures > MapLib.SideMenu.NUM_OVERLAYS ) then
			for j=MapLib.SideMenu.NUM_OVERLAYS+1, neededTextures do
				MapLibSideMenuFullscreen:CreateTexture("MapLibSideMenuOverlay"..j, "ARTWORK");
			end
			MapLib.SideMenu.NUM_OVERLAYS = neededTextures;
		end
		for j=1, numTexturesTall do
			if ( j < numTexturesTall ) then
				texturePixelHeight = 256;
				textureFileHeight = 256;
			else
				texturePixelHeight = mod(textureHeight, 256);
				if ( texturePixelHeight == 0 ) then
					texturePixelHeight = 256;
				end
				textureFileHeight = 16;
				while(textureFileHeight < texturePixelHeight) do
					textureFileHeight = textureFileHeight * 2;
				end
			end
			for k=1, numTexturesWide do
				textureCount = textureCount + 1;
				texture = getglobal("MapLibSideMenuOverlay"..textureCount);
				if ( k < numTexturesWide ) then
					texturePixelWidth = 256;
					textureFileWidth = 256;
				else
					texturePixelWidth = mod(textureWidth, 256);
					if ( texturePixelWidth == 0 ) then
						texturePixelWidth = 256;
					end
					textureFileWidth = 16;
					while(textureFileWidth < texturePixelWidth) do
						textureFileWidth = textureFileWidth * 2;
					end
				end
				texture:SetWidth(texturePixelWidth/256*58);
				texture:SetHeight(texturePixelHeight/256*58);
				texture:SetTexCoord(0, texturePixelWidth/textureFileWidth, 0, texturePixelHeight/textureFileHeight);
				texture:SetPoint("TOPLEFT", offsetX/256*58 + (58 * (k-1)) + 78, -(offsetY/256*58 + (58 * (j - 1))) - 24);
				texture:SetTexture(textureName..(((j - 1) * numTexturesWide) + k));
				texture:Show();
			end;
		end;
	end;
	for i=textureCount+1, MapLib.SideMenu.NUM_OVERLAYS do
		getglobal("MapLibSideMenuOverlay"..i):Hide();
	end;
end;

MapLib.SideMenu.MapOnMouseDown = function()
	MapLibSideMenuMapOverlayButton:SetScript("OnUpdate", MapLib.SideMenu.MapOnUpdate);
end;

MapLib.SideMenu.MapOnMouseUp = function()
	MapLibSideMenuMapOverlayButton:SetScript("OnUpdate", nil);
end;
